package business.impl;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import model.TAuth;
import model.TAuthTRole;
import model.TRole;
import model.TUserTRole;
import util.PageData;
import business.basic.*;
import business.dao.AuthDAO;

public class AuthDAOImpl implements AuthDAO {

	private HiberBaseDAOImpl impl;
	

	public void setImpl(HiberBaseDAOImpl impl) {
		this.impl = impl;
	}
	
	@Override
	public PageData getAuthList(String authname,int pageindex,int pagesize) {
		String hql ="from TAuth where auth like '%'||?||'%' order by id desc";
		Object[] param = {authname};
		PageData data = impl.select(hql, param, pageindex, pagesize);
		return data;
	}

	@Override
	public boolean addAuth(TAuth auth) {
		Object key = impl.insert(auth);
		if(key != null){
			return true;
		}
		return false;
	}

	@Override
	public boolean editAuth(TAuth auth) {
		return impl.edit(auth);
	}

	@Override
	public boolean deleAuth(int id) {
		String sql = "delete from T_Auth_T_Role where authid = ?";
		Object[] param = {id};
		if(impl.delete(sql, param)){
			if(impl.delete(TAuth.class, id)){
				return true;
			}
		}
		
		return false;
	}

	@Override
	public Object addRole(TRole role) {
		Object key = impl.insert(role);
		return key;
	}

	@Override
	public boolean editRole(TRole role) {
		return impl.edit(role);
	}

	@Override
	public boolean deleRole(int id) {
		String sql = "delete from T_Auth_T_Role where roleid = ?";
		String sql1 = "delete from T_User_T_Role where roleid = ?";
		Object[] param = {id};
		if(impl.delete(sql, param)){
			if(impl.delete(sql1,param)){
				if(impl.delete(TRole.class, id)){
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean addRoleAuth(TAuthTRole authrole) {
		Object key = impl.insert(authrole);
		if(key != null){
			return true;
		}
		return false;
	}

	@Override
	public PageData getRoleList(String rolename, int pageindex, int pagesize) {
		String hql ="from TRole where rolename like '%'||?||'%' order by id desc";
		Object[] param = {rolename};
		PageData data = impl.select(hql, param, pageindex, pagesize);
		return data;
	}
	public static void main(String args[]){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		AuthDAOImpl ubdao = ctx.getBean("authdaoimpl",AuthDAOImpl.class);
		List list = ubdao.getUserRoleByuserid(5);
		System.out.print(list.size());
	}

	@Override
	public List getRoleAuthbyRoleid(int roleid) {
		String hql = "from TAuthTRole where roleid = ? order by id desc";
		Object[] param = {roleid};
		List list = impl.select2(hql, param);
		return list;
	}

	@Override
	public boolean deleRoleAuth(int authid, int roleid) {
		String sql = "delete from T_Auth_T_Role where roleid = ? and authid=?";
		Object[] param = {roleid,authid};
		if(impl.delete(sql, param)){
			return true;
		}
		return false;
	}

	@Override
	public List getUserRoleByuserid(int userid) {
		String hql = "from TUserTRole where userid = ? order by id desc";
		Object[] param = {userid};
		List list = impl.select2(hql, param);
		return list;
	}

	@Override
	public boolean addUserRole(TUserTRole userrole) {
		Object key = impl.insert(userrole);
		if(key != null){
			return true;
		}
		return false;
	}

	@Override
	public boolean deleUserRole(int roleid, int userid) {
		String sql = "delete from T_User_T_Role where roleid = ? and userid=?";
		Object[] param = {roleid,userid};
		if(impl.delete(sql, param)){
			return true;
		}
		return false;
	}

}
